package com.turnengine.client.local.unit.command;

import com.turnengine.client.local.unit.bean.IStorageGroup;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Set Storage Group.
 */
public class SetStorageGroup implements ISetStorageGroup {

	/** The command id. */
	public static final short COMMAND_ID = 1902;

	/** The login id. */
	private long loginId;
	/** The instance id. */
	private int instanceId;
	/** The group. */
	private IStorageGroup group;

	/**
	 * Creates a new Set Storage Group.
	 */
	public SetStorageGroup() {
	}

	/**
	 * Creates a new Set Storage Group.
	 * @param loginId the login id
	 * @param instanceId the instance id
	 * @param group the group
	 */
	public SetStorageGroup(long loginId, int instanceId, IStorageGroup group) {
		setLoginId(loginId);
		setInstanceId(instanceId);
		setGroup(group);
	}

	/**
	 * Creates a new Set Storage Group.
	 * @param iSetStorageGroup the i set storage group
	 */
	public SetStorageGroup(ISetStorageGroup iSetStorageGroup) {
		setLoginId(iSetStorageGroup.getLoginId());
		setInstanceId(iSetStorageGroup.getInstanceId());
		setGroup(iSetStorageGroup.getGroup());
	}

	/**
	 * Returns the login id.
	 * @return the login id.
	 */
	public long getLoginId() {
		return loginId;
	}

	/**
	 * Returns the instance id.
	 * @return the instance id.
	 */
	public int getInstanceId() {
		return instanceId;
	}

	/**
	 * Returns the group.
	 * @return the group.
	 */
	public IStorageGroup getGroup() {
		return group;
	}

	/**
	 * Sets the login id.
	 * @param loginId the login id to set.
	 */
	public void setLoginId(long loginId) {
		this.loginId = loginId;
	}

	/**
	 * Sets the instance id.
	 * @param instanceId the instance id to set.
	 */
	public void setInstanceId(int instanceId) {
		this.instanceId = instanceId;
	}

	/**
	 * Sets the group.
	 * @param group the group to set.
	 */
	public void setGroup(IStorageGroup group) {
		if (group == null) {
			throw new NullPointerException("group");
		}
		this.group = group;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(1153, 37);
		hash.append(loginId);
		hash.append(instanceId);
		hash.append(group);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof SetStorageGroup) {
			SetStorageGroup compare = (SetStorageGroup) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.loginId, compare.loginId);
			equals.append(this.instanceId, compare.instanceId);
			equals.append(this.group, compare.group);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(ISetStorageGroup compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.loginId, compare.getLoginId());
		builder.append(this.instanceId, compare.getInstanceId());
		builder.append(this.group, compare.getGroup());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		string.append("loginId", loginId);
		string.append("instanceId", instanceId);
		if (group != null) {
			string.append("group", group);
		}
		return string.toString();
	}

	public short getCommandId() {
		return COMMAND_ID;
	}
}
